
cd /tmp
VER="10.6.5"

[ -e mariadb-10.6.5-linux-systemd-x86_64.tar.gz ] || wget https://mirrors.aliyun.com/mariadb//mariadb-$VER/bintar-linux-systemd-x86_64/mariadb-$VER-linux-systemd-x86_64.tar.gz
rm -rf mariadb-$VER-linux-systemd-x86_64
tar xf mariadb-$VER-linux-systemd-x86_64.tar.gz
rm -rf mysql

PORT="${PORT:-3306}"

id -u mysql &>/dev/null || useradd mysql

DIR=/usr/local/mysql
mv mariadb-$VER-linux-systemd-x86_64 $DIR

DB=/data/mydb-$PORT
$DIR/scripts/mysql_install_db --user=mysql --datadir=$DB

sudo -u mysql $DIR/bin/mysqld --datadir=/data/mydb-$PORT --socket=/tmp/mysql-$PORT.sock &
sleep 2
cat <<EOF | $DIR/bin/mysql --socket /tmp/mysql-$PORT.sock
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'toor';
    CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'toor';
    CREATE USER 'root'@'%' IDENTIFIED BY 'toor';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
EOF

# 启动
# /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.ini

$DIR/bin/mysqladmin --socket /tmp/mysql-$PORT.sock shutdown
sleep 2

wget https://gitee.com/pandora/install/raw/master/mysql.service -O /etc/systemd/system/mysql-$PORT.service

systemctl enable mysql-$PORT
systemctl start mysql-$PORT